package com.mipo.shiro.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mipo.mybatis.mysql.mapper.RoleMapper;
import com.mipo.mybatis.mysql.mapper.UserMapper;
import com.mipo.mybatis.mysql.mapper.UserRoleMapper;
import com.mipo.mybatis.mysql.model.po.Role;
import com.mipo.mybatis.mysql.model.po.UserRole;
import com.mipo.mybatis.mysql.model.qo.RoleDto;
import com.mipo.mybatis.mysql.model.qo.UserDto;
import com.mipo.mybatis.mysql.model.qo.UserRoleDto;
import com.mipo.shiro.dao.UserDao;
import com.mipo.shiro.service.UserService;

@Service("userService-userin")
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDao userDao;
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private UserRoleMapper userRoleMapper;
    
    @Autowired
    private RoleMapper roleMapper;

    @Override
	public com.mipo.mybatis.mysql.model.po.User getByUsername(String username) {
    	UserDto dto = new UserDto();
    	dto.setUsername(username);
    	com.mipo.mybatis.mysql.model.po.User user2 = userMapper.get(dto);
    	return user2;
    	
//    	User user = new User();
//    	user.setId(1001);
//    	user.setUsername("lijie_test");
//    	user.setPassword("123456");
//    	return user;
		//return userDao.getByUsername(username);
	}

    @Override
    public Set<String> getRoles(String username) {
    	Set<String> roles = new HashSet<String>();
    	
    	UserDto dto = new UserDto();
    	dto.setUsername(username);
    	com.mipo.mybatis.mysql.model.po.User user2 = userMapper.get(dto);
    	
    	UserRoleDto userRoleDto = new UserRoleDto();
    	userRoleDto.setUser_id(user2.getId());
    	List<UserRole> userRoles = userRoleMapper.list(userRoleDto);
    	
    	for(UserRole userRole : userRoles) {
    		RoleDto roleDto = new RoleDto();
        	roleDto.setId(userRole.getRoleId());
        	Role role = roleMapper.get(roleDto);
        	String roleEnName = role.getRoleEnName();
        	roles.add(roleEnName);
    	}
    	//roles.add("teacher");
    	return roles;
    }
    
    @Override
    public Set<String> getPermissions(String username) {
    	Set<String> permissions = new HashSet<String>();
    	permissions.add("user:create");
    	return permissions;
    	//return userDao.getPermissions(username);
    }

}